package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.LklMerchant;
import com.xmy.cultivate.entity.LklMerchantSet;
import com.xmy.cultivate.entity.LklMerchantStudent;
import com.xmy.cultivate.entity.LklMerchantTeacher;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-03-21
 */
@RestController
@RequestMapping("/api/lklMerchantTeacher")
public class LklMerchantTeacherController {

    @Autowired
    ILklMerchantStudentService iLklMerchantStudentService;
    @Autowired
    ILklMerchantService iLklMerchantService;
    @Autowired
    ILklMerchantTeacherService iLklMerchantTeacherService;
    @Autowired
    IQuarterService iQuarterService;
    @Autowired
    ILklMerchantSetService iLklMerchantSetService;

    @GetMapping("/getLklMerchantTeacherList")
    public Result getLklMerchantTeacherList(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                     @RequestParam(value = "subjectsId", required = false) String subjectsId,  //科目
                                     @RequestParam(value = "name", required = false) String name, //教师名称
                                     @RequestParam(value = "id", required = true) String id,
                                     @RequestParam(value = "yearPart", required = false) String yearPart,  //年份
                                     @RequestParam(value = "quarterNum", required = false) String quarterNum //学期

    ){
        IPage<LklMerchantTeacher> iPage = new Page<>(pageNum, pageSize);

       iLklMerchantTeacherService.getLklMerchantTeacherList(iPage, subjectsId, name, id,yearPart, quarterNum);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), "查询成功");

    }

    @PostMapping("/saveLklMerchantTeacher")
    public Result saveLklMerchantTeacher(@RequestBody LklMerchantTeacher lklMerchantTeacher){

        if (lklMerchantTeacher.getTeacherId() == null){
            return new Result(Code.EXP_ERROR, null, "教师id不能为空");
        }
//        QueryWrapper<LklMerchant> merchantQueryWrapper = new QueryWrapper<>();
//        merchantQueryWrapper.eq("id", lklMerchantTeacher.getMerchantId());
//        merchantQueryWrapper.eq("year_part", lklMerchantTeacher.getYearPart());
//        merchantQueryWrapper.eq("quarter_num", lklMerchantTeacher.getQuarterNum());
//        LklMerchant existLklMerchant = iLklMerchantService.getOne(merchantQueryWrapper);

        LklMerchant merchant = iLklMerchantService.getById(lklMerchantTeacher.getMerchantId());
        if (merchant == null){
            return new Result(Code.EXP_ERROR, null, "该商户不存在，添加失败");
        }
        QueryWrapper<LklMerchantTeacher> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("merchant_id", lklMerchantTeacher.getMerchantId());
        queryWrapper.eq("school_id", merchant.getSchoolId());
        queryWrapper.eq("teacher_id", lklMerchantTeacher.getTeacherId());

        long count = iLklMerchantTeacherService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该教师已存在，无法添加");
        }
//        LklMerchantTeacher existLklMerchantTeacher = iLklMerchantTeacherService.existLklMerchantTeacher(lklMerchantTeacher.getTeacherId());
//        if (existLklMerchantTeacher != null){
//            return new Result(Code.EXP_ERROR, null, "该教师已存在"+existLklMerchantTeacher.getSchoolName()+"的"+existLklMerchantTeacher.getMerchantName()+"商户下，无法添加");
//        }
        boolean save = iLklMerchantTeacherService.save(lklMerchantTeacher);
        if (save) {
            return new Result(Code.OK, "", "添加成功");
        }
        return new Result(Code.ERROR,"","添加失败");
    }

    //删除
    @DeleteMapping("/deleteLklMerchantTeacher")
    public Result deleteLklMerchantTeacher(@RequestBody Map reMap) {

        List<String> ids = (List<String>) reMap.get("ids");
        QueryWrapper<LklMerchantTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        boolean b = iLklMerchantTeacherService.remove(queryWrapper);
        if (b) {
            return new Result(Code.DELETE_OK, "", Code.DELETE_OK_MSG);
        }
        return new Result(Code.DELETE_ERROR, "", Code.DELETE_ERROR_MSG);
    }

    //编辑
    @PutMapping("/updateLklMerchantTeacher")
    public Result updateLklMerchantTeacher(@RequestBody LklMerchantTeacher lklMerchantTeacher){

        if (lklMerchantTeacher.getTeacherId() == null){
            return new Result(Code.EXP_ERROR, null, "教师id不能为空");
        }
        LklMerchantTeacher lklMerchantTeacher1 = iLklMerchantTeacherService.getById(lklMerchantTeacher.getId());
        if (lklMerchantTeacher1 == null){
            return new Result(Code.EXP_ERROR, null, "该教师不存在，编辑失败");
        }

//        QueryWrapper<LklMerchant> merchantQueryWrapper = new QueryWrapper<>();
//        merchantQueryWrapper.eq("id", lklMerchantTeacher1.getMerchantId());
//        merchantQueryWrapper.eq("year_part", lklMerchantTeacher.getYearPart());
//        merchantQueryWrapper.eq("quarter_num", lklMerchantTeacher.getQuarterNum());
//        LklMerchant existLklMerchant = iLklMerchantService.getOne(merchantQueryWrapper);

        LklMerchant merchant = iLklMerchantService.getById(lklMerchantTeacher1.getMerchantId());
        if (merchant == null){
            return new Result(Code.EXP_ERROR, null, "该商户不存在，添加失败");
        }

        QueryWrapper<LklMerchantTeacher> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("school_id", lklMerchantTeacher.getSchoolId());
        queryWrapper.eq("school_id", merchant.getSchoolId());
        queryWrapper.eq("teacher_id", lklMerchantTeacher.getTeacherId());
        long count = iLklMerchantTeacherService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该教师已存在，无法编辑");
        }
//        LklMerchantTeacher existLklMerchantTeacher = iLklMerchantTeacherService.existLklMerchantTeacher(lklMerchantTeacher.getTeacherId());
//        if (existLklMerchantTeacher != null){
//            return new Result(Code.EXP_ERROR, null, "该教师已存在"+existLklMerchantTeacher.getSchoolName()+"的"+existLklMerchantTeacher.getMerchantName()+"商户下，无法编辑");
//        }
        boolean update = iLklMerchantTeacherService.updateById(lklMerchantTeacher);
        if (update) {
            return new Result(Code.UPDATE_OK, "", "编辑成功");
        }
        return new Result(Code.UPDATE_ERROR,"","编辑失败");
    }


    @GetMapping("/getLklMerchantTeacherListDetails")
    public Result getLklMerchantTeacherListDetails(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                            @RequestParam(value = "id", required = true) String id,
                                            @RequestParam(value = "name", required = false) String name, //学员姓名
                                            @RequestParam(value = "yearPart", required = false) String yearPart,  //年份
                                            @RequestParam(value = "quarterNum", required = false) String quarterNum, //学期
                                            @RequestParam(value = "subjectsId", required = false) Long subjectsId //科目


    ){
        IPage<LklMerchantSet> iPage = new Page<>(pageNum, pageSize);

        LklMerchantTeacher lklMerchantTeacher = iLklMerchantTeacherService.getById(id);
        Long teacherId = lklMerchantTeacher.getTeacherId();
        Long merchatId = lklMerchantTeacher.getMerchantId();

        iLklMerchantTeacherService.getLklMerchantTeacherListDetails(iPage, yearPart, quarterNum, teacherId, name);

        for (LklMerchantSet lklMerchantSet : iPage.getRecords()){

            String merchantName = null;


             merchantName = iLklMerchantSetService.getMerchantName(yearPart, quarterNum, subjectsId, lklMerchantSet.getStudentId());
             if (merchantName != null){
                 lklMerchantSet.setMerchantName(merchantName);
             }else {
                 merchantName = iLklMerchantTeacherService.getMerchantName(merchatId);
                 lklMerchantSet.setMerchantName(merchantName);
             }

//            QueryWrapper<LklMerchantStudent> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("merchant_set_id",merchantSetId);
//            queryWrapper.eq("year_part",yearPart);
//            queryWrapper.eq("quarter_num",quarterNum);
//            //获取最后一条
//            queryWrapper.last("limit 1");
//            LklMerchantSet lklMerchantSet1 = iLklMerchantSetService.getOne(queryWrapper);
//            if (lklMerchantSet1 != null){
//                lklMerchantSet.setMerchantName(lklMerchantSet1.getMerchantName());
//            }else {
//                String ;
//            }





//            lklMerchantSet.setMerchantName(iLklMerchantService.getById(merchatId).getMerchantName());

        }


//
//        iLklMerchantTeacherService.getLklMerchantTeacherList(iPage, subjectsId, name, id);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), "查询成功");



    }

}
